#!/usr/bin/env python
#-*-coding:utf-8-*-



word_vec_path = "/home/wuzheng/Pyprojects/bsner/data/word_vec_100.txt"
char_vec_path = "/home/wuzheng/Pyprojects/bsner/data/char_vec_100.txt"
train_path = "/home/wuzheng/Pyprojects/bsner/data/seg_ner_train.txt"


def create_word_to_id(path):
    vocab_dict = {}
    vocab_dict["<PAD>"] = 0
    vocab_dict["<OOV>"] = 1

    f = open(path, errors="ignore")
    m, n = f.readline().split(" ")
    n = int(n)
    m = int(m)

    for i, line in enumerate(f):
        word = line.split()[0]
        if word not in vocab_dict:
            vocab_dict[word] = len(vocab_dict)

    print(m, n)
    print(len(vocab_dict))
    return vocab_dict

def tag_to_path(index):
    f = open(train_path)
    tag_to_id = {}
    for i,line in enumerate(f):
        line = line.strip("\n")
        if not line:
            continue
        data = line.split("\t")
        if len(data) !=4:
            print(line)
        tag = data[index]
        # print(tag)
        if tag not in tag_to_id:
            tag_to_id[tag] = len(tag_to_id)

    return {v:k for k, v in tag_to_id.items()}



if __name__ == '__main__':

    word_to_id = create_word_to_id(word_vec_path)
    char_to_id = create_word_to_id(char_vec_path)

    id_to_seg = tag_to_path(1)
    id_to_pos = tag_to_path(2)
    id_to_ner = tag_to_path(-1)

    import os
    import pickle

    with open(os.path.join("/home/wuzheng/Pyprojects/bsner/data", "maps.pkl"), 'wb') as f:
        out_obj = {"char_map": char_to_id,
                   "word_map": word_to_id,
                   "seg_map": id_to_seg,
                   "pos_map": id_to_pos,
                   "ner_map": id_to_ner
                  }
    pickle.dump(out_obj, f)
